package whu.edu.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import whu.edu.demo.domain.Product;
import whu.edu.demo.exception.ProductException;
import whu.edu.demo.service.ProductService;

import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    ProductService service;
    @GetMapping("")
    public List<Product> getC(String name, Integer price){
        return service.getProduct(name,price);
    }



    @GetMapping("/{id}")
    public ResponseEntity<Product> findCs(@PathVariable int id){
        Product result = service.getProduct(id);
        if(result == null){return ResponseEntity.noContent().build();}
        else{return ResponseEntity.ok(result);}
    }


    @PostMapping("/modify")
    public Product addC(@RequestBody Product c) throws ProductException {
        return service.addProduct(c);
    }


    @DeleteMapping("/modify/{id}")
    public void deleteC(@PathVariable int id){
        service.deleteProduct(id);
    }


    @PutMapping("/modify/{id}")
    public void putC(@PathVariable int id, @RequestBody Product c)throws ProductException{
        service.updateProduct(id, c);
    }
}


